import 'package:flutter/material.dart';
import 'package:myturngame/Theme.dart';
import 'package:myturngame/common/game_global.dart';
import 'package:myturngame/state/SelectSkillChangeNotifier.dart';
import 'package:myturngame/widget/text_decoration.dart';
import 'package:provider/provider.dart';

import '../../bean/skill/skill.dart';

///技能栏
///@author Administrator
///@date 2023年2月10日00:05:44

class DialogSkill extends StatefulWidget {
  DialogSkill({Key? key}) : super(key: key);

  @override
  State<DialogSkill> createState() => _DialogSkillState();
}

class _DialogSkillState extends State<DialogSkill> {
  @override
  Widget build(BuildContext context) {
    return Dialog(
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          const SkillLookLayout(),
          const Divider(height: 10),
          Row(
            mainAxisSize: MainAxisSize.min,
            children: [
              OutlinedButton(onPressed: () {}, child: const Text("遗忘"))
            ],
          )
        ],
      ),
    );
  }
}

class SkillLookLayout extends StatefulWidget {
  const SkillLookLayout({Key? key}) : super(key: key);

  @override
  State<SkillLookLayout> createState() => _SkillLookLayoutState();
}

class _SkillLookLayoutState extends State<SkillLookLayout> {
  // Skill? selectSkill;

  @override
  Widget build(BuildContext context) {
    // 技能描述
    var skillText = const Text("技能描述");
    var selectSkill =
        Provider.of<SelectSkillChangeNotifier>(context).selectSkill;
    if (selectSkill != null) {
      skillText = Text.rich(TextSpan(children: selectSkill.description()));
    }

    return ConstrainedBox(
      constraints: const BoxConstraints(
        maxWidth: 350,
      ),
      child: Padding(
        padding: const EdgeInsets.all(10),
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            BorderContainer(
              skillText,
              width: double.infinity,
              height: 100,
            ),
            const Divider(height: 10),
            BorderContainer(
              GridView.count(
                  crossAxisCount: 6,
                  crossAxisSpacing: 3,
                  children: GameGlobal.player.skills
                      .map((e) => itemLayout(e, selectSkill))
                      .toList()),
              width: double.infinity,
              height: 100,
            ),
          ],
        ),
      ),
    );
  }

  Widget itemLayout(Skill e, Skill? selectSkill) {
    var color = const Color(0xFF000000);
    var borderWidth = 1.0;
    if (selectSkill == e) {
      color = Colors.red;
      borderWidth = 2;
    }
    return GestureDetector(
      onTap: () {
        Provider.of<SelectSkillChangeNotifier>(context, listen: false)
            .selectSkill = e;
      },
      child: BorderContainer(
        Center(
          child: Text(
            e.name,
            style: textStyle4,
          ),
        ),
        borderColor: color,
        borderWidth: borderWidth,
      ),
    );
  }
}
